<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/27
 * Time: 14:58
 */

namespace app\model;
use app\model\StationInfo as ModelStationInfo;
use app\model\StationMemberInfo as ModelStationMemberInfo;
use app\model\UserOrder as ModelUserOrder;
use think\Db;
use think\Request;

class StationOrder extends Common
{
    protected static $obj;

    public function _initialize()
    {
        parent::_initialize();
        $this->db_name = 'station_order';
    }

    /**
     * 类入口
     * @return StationOrder
     */
    public static function getInstance()
    {
        if (is_null(self::$obj))
            self::$obj = new self();
        return self::$obj;
    }

    /**
     * 订单站点信息
     */
    public function getStationInfo($arr_where){
        $data = Db::name($this->db_name)
            ->where($arr_where)
            ->field('station_info.id,station_info.name,station_info.express_id,station_box_order.box_order_number AS boxOrderNumber,station_info.mobile')
            ->join('station_info','station_info.id=station_order.station_id','left')
            ->join('station_box_order','station_box_order.station_id = station_order.station_id and find_in_set (station_order.order_id,station_box_order.box_content)','left')
            ->find();
        return $data;
    }
    /*
     * 添加站点订单
     * $isPick  1：发件站点 2:收件站点
     */
    public function stationOrderAdd($operatorId,$orderId,$isPick,$receiveStationId)
    {
        $data['station_id'] = $this->getStationId($operatorId);
        $data['order_id'] = $orderId;
        $data['is_pick'] = $isPick;
        $data['order_number'] = ModelUserOrder::getInstance()->toValue(['order_id'=>$orderId],'order_sn');
        $data['state'] = ModelUserOrder::getInstance()->toValue(['order_id'=>$orderId],'order_state');
        $data['operator_id'] = $operatorId;
        $data['station_name'] = $this->getStationName($operatorId);
        $data['is_unusual'] = ModelUserOrder::getInstance()->toValue(['order_id'=>$orderId],'is_unusual');
        $data['receive_station_id'] = $receiveStationId;
        $data['receive_station_name'] = ModelStationInfo::getInstance()->toValue(['id'=>$receiveStationId],'branch_name');

        return $this->toAdd($data);

    }

    /*
     * 获取站点id
     */
    public function getStationId($operatorId)
    {
        return ModelStationMemberInfo::getInstance()->toValue(['id'=>$operatorId],'station_id');
    }
    /*
     * 获取站点名称
     */
    public function getStationName($operatorId)
    {
        $stationId = $this->getStationId($operatorId);
        return ModelStationInfo::getInstance()->toValue(['id'=>$stationId],'branch_name');
    }
}